<?php
/**
 * File Name: Member.php
 *
 * Author: Larry
 * Email: lifushui@qq.com
 * Qq: 197508705
 * Url: www.feihuang.org
 */

namespace App\Services\Wechat;
use App\Models\Member as Mem;
use App\Models\MemberScoreLog;
use App\Models\Score;
use App\Models\WechatSet;
use App\Support\Wechat\User;
use Request;
use App\Services\Notice\TemplateNotice;


class Member
{
    public function __construct(Mem $member, WechatSet $wechatSet, MemberScoreLog $scoreLog, Score $score, TemplateNotice  $templateNotice)
    {
        $this->member = $member;
        $this->wechat = $wechatSet->getWechatSet();
        $this->scorelog = $scoreLog;
        $this->score = $score;
        $this->templateNotice = $templateNotice;
    }
    /**
     * 添加会员信息
     *
     */
    public function addMember($openid, $referrer = 0)
    {
        $auth = new User($this->wechat['app_id'], $this->wechat['app_secret']);
        $user = $auth->get($openid);
        //用户详情入库
        $cache = $this->member->getOpenid($openid);
        $user = iterator_to_array($user);
        $openid = $user['openid'];

        if(!$cache)
        {
            $user['referrer'] = $referrer;
        }
        $user['ip'] = Request::getClientIp();
        $where['openid'] = $openid;
        $result = $this->member->updateOrCreate($where, $user);
        //给推荐人加积分
        if($referrer && !$cache)
        {
            //更新积分
            $score_in = 20;
            $score = $this->score->getOne(['uid'=>$referrer]);
            $scoreData['total_score'] = $score['total_score'] + $score_in;
            $scoreData['rest_score'] = $score['rest_score'] + $score_in;
            $this->score->up(['uid'=>$referrer], $scoreData);
            //写入积分日志
            $sd['uid'] = $referrer;
            $sd['score'] = $score_in;
            $sd['scoreby'] = 2;
            $sd['remark'] = "{$user['nickname']}关注";
            $this->scorelog->add($sd);
            //发送积分消息
            $refer = $this->member->getMember($referrer);
            $text = "{$user['nickname']}关注了您的邀请代言";
            $this->sendScoreMsg($refer['openid'], $score_in, $scoreData['rest_score'], $text);
        }
        return $result;
    }

    /**
     *关注
     */
    public function subscribe($message)
    {
        $referrer = 0;
        if($message->EventKey)
        {
            list(,$referrer) = explode('_',$message->EventKey);
        }
        //会员信息入库
        $this->addMember($message->FromUserName, $referrer);
        $msg = "呀！一不小心我们就成为了微信好友啦，我在这里恭候大驾多时，从现在开始您可以随时随地进行【一元捡宝】的微信用户专享服务！请直接点击底部菜单，找寻属于你的专属宝物，畅享夺宝过程中的乐趣！\n\n<a href='http://yiyuanshibao.com/w/goods/show/5'>一元抢走iphone6s>>></a>\n\n<a href='http://t.cn/RqWCmsb'>【捡宝攻略】 据说92%的人看过都中奖了 </a> ";
        return $msg;
    }

    /**
     *取消关注
     */
    public function unsubscribe($message)
    {
        $openid = $message->FromUserName;
        $user = $this->member->getOne(['openid'=>$openid]);
        //给推荐人减积分
        if(isset($user->referrer) && $user->referrer){
            $refer = $this->member->getOne(['id'=>$user->referrer]);
            //更新积分
            $score_in = -20;
            $score = $this->score->getOne(['uid'=>$refer->id]);
            $scoreData['total_score'] = ($score['total_score'] + $score_in) < 0 ? 0 : $score['total_score'] + $score_in;
            $scoreData['rest_score'] = ($score['rest_score'] + $score_in) < 0 ? 0 : $score['rest_score'] + $score_in;
            $this->score->up(['uid'=>$refer->id], $scoreData);
            //写入积分日志
            $sd['uid'] = $refer->id;
            $sd['score'] = $score_in;
            $sd['scoreby'] = 4;
            $sd['remark'] = "{$user['nickname']}取消关注";
            $this->scorelog->add($sd);
            //发送积分消息
            $text = "{$user['nickname']}取消了关注";
            $this->sendScoreMsg($refer['openid'], $score_in, $scoreData['rest_score'], $text);
        }

        $data['subscribe'] = 0;
        $data['referrer'] = 0;
        $where['openid'] = $openid;
        $this->member->up($where, $data); //更新用户状态
        $this->member->delMemberMc($user->id);//清除缓存

        return true;
    }

    /**
     * 发送积分消息
     */
    public function sendScoreMsg($openid, $score_in, $rest_score, $text)
    {
        //获取关注状态
        $auth = new User($this->wechat['app_id'], $this->wechat['app_secret']);
        $member = $auth->get($openid);
        if(isset($member['subscribe']) && $member['subscribe'] == 1)
        {
            $openid = $member['openid'];
            $url = action('Wechat\MemberController@getIndex');
            $first = "尊敬的{$member['nickname']}，您的剩余积分有新的变动";
            $remark = '100积分可以兑换一个夺宝币，兑换的夺宝币即可免费参与夺宝';
            $addtime = date('Y年m月d日 H:i');
            $res = $this->templateNotice->sendScoreMassage($openid, $url,$first,$addtime,$score_in,$text,$rest_score,$remark);
            return $res;
        }
        return false;

    }
}